import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Delete,
  Query,
  UseGuards,
  Request,
} from '@nestjs/common';
import { ReviewsService } from './reviews.service';
import { CreateReviewDto } from './dto/create-review.dto';
import { UpdateReviewDto } from './dto/update-review.dto';
import { QueryReviewsDto } from './dto/query-reviews.dto';
import { MarkHelpfulDto } from './dto/mark-helpful.dto';
import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';
import { Roles } from '../../common/decorators/roles.decorator';
import { Role } from '../../common/decorators/roles.decorator';
import { RolesGuard } from '../../common/guards/roles.guard';

@Controller('reviews')
export class ReviewsController {
  constructor(private readonly reviewsService: ReviewsService) {}

  /**
   * 创建评论（需要登录）
   */
  @Post()
  @UseGuards(JwtAuthGuard)
  create(@Request() req, @Body() createReviewDto: CreateReviewDto) {
    return this.reviewsService.create(req.user.userId, createReviewDto);
  }

  /**
   * 查询评论列表（公开）
   */
  @Get()
  findAll(@Query() query: QueryReviewsDto) {
    return this.reviewsService.findAll(query);
  }

  /**
   * 获取工具的评分统计（公开）
   */
  @Get('tool/:toolId/stats')
  getToolRatingStats(@Param('toolId') toolId: string) {
    return this.reviewsService.getToolRatingStats(+toolId);
  }

  /**
   * 检查用户是否已评论（需要登录）
   */
  @Get('tool/:toolId/check')
  @UseGuards(JwtAuthGuard)
  async checkUserReview(@Request() req, @Param('toolId') toolId: string) {
    const hasReviewed = await this.reviewsService.hasUserReviewed(
      req.user.userId,
      +toolId,
    );
    return { hasReviewed };
  }

  /**
   * 查询单个评论（公开）
   */
  @Get(':id')
  findOne(@Param('id') id: string) {
    return this.reviewsService.findOne(+id);
  }

  /**
   * 更新评论（仅作者）
   */
  @Patch(':id')
  @UseGuards(JwtAuthGuard)
  update(
    @Request() req,
    @Param('id') id: string,
    @Body() updateReviewDto: UpdateReviewDto,
  ) {
    return this.reviewsService.update(req.user.userId, +id, updateReviewDto);
  }

  /**
   * 删除评论（仅作者和管理员）
   */
  @Delete(':id')
  @UseGuards(JwtAuthGuard)
  remove(@Request() req, @Param('id') id: string) {
    // 这里需要在service中检查是否是作者或管理员
    return this.reviewsService.remove(req.user.userId, +id);
  }

  /**
   * 标记评论为有帮助/无帮助（需要登录）
   */
  @Post(':id/helpful')
  @UseGuards(JwtAuthGuard)
  markHelpful(@Param('id') id: string, @Body() markHelpfulDto: MarkHelpfulDto) {
    return this.reviewsService.markHelpful(+id, markHelpfulDto.helpful);
  }

  /**
   * 审核评论（仅管理员）
   */
  @Patch(':id/approve')
  @Roles(Role.ADMIN, Role.SUPER_ADMIN)
  @UseGuards(JwtAuthGuard, RolesGuard)
  approveReview(@Param('id') id: string) {
    return this.reviewsService.updateStatus(+id, 'approved');
  }

  /**
   * 拒绝评论（仅管理员）
   */
  @Patch(':id/reject')
  @Roles(Role.ADMIN, Role.SUPER_ADMIN)
  @UseGuards(JwtAuthGuard, RolesGuard)
  rejectReview(@Param('id') id: string) {
    return this.reviewsService.updateStatus(+id, 'rejected');
  }

  /**
   * 设置为精选评论（仅管理员）
   */
  @Patch(':id/feature')
  @Roles(Role.ADMIN, Role.SUPER_ADMIN)
  @UseGuards(JwtAuthGuard, RolesGuard)
  featureReview(@Param('id') id: string) {
    return this.reviewsService.toggleFeatured(+id, true);
  }
}
